{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22562146",
   "metadata": {},
   "source": [
    "## 计算机视觉的背景"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338fbc9c",
   "metadata": {},
   "source": [
    "在早期的图像分类技术中，需要先经过专业人士对图片进行特征提取，比如图片的颜色，形状，纹理等进行提取，在使用机器学习算法对这些特征进行分类。  \n",
    "这种方法效率低下，准确度不高，很难普及。  \n",
    "后来有个叫YL的人将卷积神经网络应用在图像分类任务中，效果不错。但是受限于计算机技术和数据集的影响，也无法普遍适用。  \n",
    "现在，随着互联网的发展，计算机算力的提升，各种场景的需要，卷积神经网络的不断优化以及各种模型算法应用到计算机视觉中，处理这些问题已经不在复杂。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af0345f6",
   "metadata": {},
   "source": [
    "![](./images/3_输出形状计算.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3e9fc8b",
   "metadata": {},
   "source": [
    "## 卷积神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c198d6b4",
   "metadata": {},
   "source": [
    "卷积神经网络是计算机视觉技术最经典的模型，包含：卷积、池化、激活函数、丢弃等。可以用来做图像识别、目标检测、图像分类等工作中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1104f0a",
   "metadata": {},
   "source": [
    "![](./images/3_卷积计算动态图.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50452419",
   "metadata": {},
   "source": [
    "### 卷积计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "826441a2",
   "metadata": {},
   "source": [
    "用一个小矩阵去乘以一个大矩阵中对应部分，合并计算结果。移动一定距离计算其余部分，这样就能把大矩阵一步一步的缩小，达到提取特征的目的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0366b528",
   "metadata": {},
   "source": [
    "![](./images/3_卷积计算.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a67a8a3",
   "metadata": {},
   "source": [
    "**说明：计算结果往往还需要加上偏置项**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4e38a10",
   "metadata": {},
   "source": [
    "### 填充"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ae85c9c",
   "metadata": {},
   "source": [
    "经过一次卷积操作后，大矩阵的维度会变小，为了避免维度减小，通常会提前在大矩阵外围进行数字填充，一般填充为0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09ab939d",
   "metadata": {},
   "source": [
    "### 步幅"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a48c3aa",
   "metadata": {},
   "source": [
    "卷积每次滑动的距离，通常步幅为1,2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "099fba68",
   "metadata": {},
   "source": [
    "### 感受野"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d65e2527",
   "metadata": {},
   "source": [
    "输出特征中每个元素对应原始矩阵的区域被称为感受野"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f78b3fc",
   "metadata": {},
   "source": [
    "![](./images/3_感受野.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2646d797",
   "metadata": {},
   "source": [
    "### 通道"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38d12693",
   "metadata": {},
   "source": [
    "一张图片通常由RGB三中颜色构成，即：一张图片是由3个大矩阵叠加构成的，这三个大矩阵在计算的时候至少需要3条通道分别进行卷积计算。  \n",
    "**通道数可以多，但不能少，不知道是不是这样理解的**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b24854e8",
   "metadata": {},
   "source": [
    "![](./images/3_通道.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ba850ee",
   "metadata": {},
   "source": [
    "### 池化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f7a7c01",
   "metadata": {},
   "source": [
    "与卷积不同的操作，将大矩阵中的某一部分数据用某一个统计指标代替，比如：平均值、最大值、最小值等。这样做即使原始数据有细微的改变，对池化后的数据影响不大。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f7fd80d",
   "metadata": {},
   "source": [
    "![](./images/3_池化.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da3e7fc9",
   "metadata": {},
   "source": [
    "### ReLU激活函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4d6e758",
   "metadata": {},
   "source": [
    "Sigmoid函数在反向传播的过程中，会造成梯度衰减，目前在神经网络模型中使用较多的激活函数是ReLU函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17e1b2f2",
   "metadata": {},
   "source": [
    "![](./images/3_ReLU和Sigmoid.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a47937a",
   "metadata": {},
   "source": [
    "### 拟归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bc55c95",
   "metadata": {},
   "source": [
    "我们在训练样本前会对样本数据进行归一化处理，使其训练更快，模型效果更好。对于神经网络模型，由于参数需要不断更新，刚开始输入的数据满足标准化处理，但是对于靠后的层，接受到的数据是很复杂的。拟归一化就是解决这种问题。  \n",
    "他可以使神经网络中层与层之间传递数据变得更加稳定，加快计算速度，防止过拟合。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c690393",
   "metadata": {},
   "source": [
    "### 丢弃法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9a08963",
   "metadata": {},
   "source": [
    "神经网络算法中庞大的神经元会导致数据过拟合，丢弃法就是随机选择一部分神经元，将其输出结果设置为0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc2343c",
   "metadata": {},
   "source": [
    "![](./images/3_丢弃法.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d8228ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a6ab887",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7ba10ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "651c776f",
   "metadata": {},
   "source": [
    "## 案例"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09baf4c9",
   "metadata": {},
   "source": [
    "### 简单的黑白边界检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "52e40382",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import paddle\n",
    "from paddle.nn import Conv2D\n",
    "from paddle.nn.initializer import Assign\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fc4af9c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "22b293b4",
   "metadata": {},
   "source": [
    "<font color=\"red\">**设置宽度方向的卷积核为[1,0,-1]，此卷机核会将宽度方向间隔为1的两个像素点数值相减**</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "875ec63b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建初始化权重参数w\n",
    "w = np.array([1,0,-1],dtype='float32')\n",
    "# 将权重参数调整成维度为[cout,cin,kh,kw]的四维张量\n",
    "w = w.reshape([1,1,1,3])\n",
    "# 创建卷积算子，设置输出通道数，卷积核大小，和初始化权重参数\n",
    "# 创建卷积算子的时候，从 numpy.ndarray初始化卷积参数\n",
    "conv = Conv2D(in_channels=1,out_channels=1,kernel_size=[1,3],weight_attr=paddle.ParamAttr(\n",
    "    initializer=Assign(value=w)\n",
    "))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e126c96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[ 1.,  0., -1.]]]], dtype=float32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c85a0045",
   "metadata": {},
   "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zDLlADczy9T/AxHQF3U+ZCf0AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 穿件输入图片\n",
    "img = np.ones([50,50],dtype=\"float32\")\n",
    "img[:,30:] = 0.\n",
    "# 将图片调整为[N,C,H,W]的形式\n",
    "x = img.reshape([1,1,50,50])\n",
    "# 将numpy.ndarray转化为paddle中的tensor\n",
    "x = paddle.to_tensor(x)\n",
    "# 将卷积算子作用在图片上\n",
    "y = conv(x)\n",
    "# 将输出的tensor转化为numpy.ndarray\n",
    "out = y.numpy()\n",
    "f = plt.subplot(121)\n",
    "f.set_title('input image',fontsize=15)\n",
    "plt.imshow(img,cmap='gray')\n",
    "f = plt.subplot(122)\n",
    "f.set_title('output image',fontsize=15)\n",
    "plt.imshow(out.squeeze(),cmap=\"gray\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f6130310",
   "metadata": {},
   "outputs": [],
   "source": [
    "# out.squeeze()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "218.875px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
